/*
 * Created on Mar 8, 2005
 */
import java.awt.Color;
import java.util.Random;
import javax.swing.Timer;

/**
 * @author Jesse Phillips
 */
public class Objects {
	private DrawMe objDraw;
	private Timer frameRate, fallTime, round;
	private Color myColor;
	public String shape;
	public Objects player;
    private int w, h, centerX, centerY;
    private int preX, preY, x, y;
    private int g = -1, velocityY = 0, velocityX = 0, time = 0;
    private boolean increase = false, fill = true;
    
    public Objects(int x, int y, int w, int h, String shape, boolean f, DrawMe d) {
        this.w = w;
        this.h = h;
        this.x = x;
    	this.y = y;
    	this.shape = shape;
    	fill = f;
    	
    	centerX = x + (w/2);
    	centerY = y + (h/2);
    	
    	objDraw = d;
    	
    	myColor = rndColor();
    	
    	TimeTracker foo = new TimeTracker(this, false);
    	frameRate = new Timer(100, foo);
    	frameRate.start();
    	
    	foo = new TimeTracker(this, true);
    	fallTime = new Timer(100, foo);
    }
    
	public Objects(int i, String string, DrawMe objDraw) {
		this.x = i;
    	this.shape = string;
    	this.objDraw = objDraw;
    	
		TimeTracker foo = new TimeTracker(this);
    	frameRate = new Timer(100, foo);
    	frameRate.start();
	}

	public Objects(int x, int y, int w, int h, String shape, boolean f, DrawMe d, Objects player, Timer round) {
		this.w = w;
        this.h = h;
        this.x = x;
    	this.y = y;
    	this.shape = shape;
    	this.player = player;
    	this.round = round;
    	fill = f;
    	objDraw = d;
    	TimeTracker foo;
    	
    	centerX = x + (w/2);
    	centerY = y + (h/2);
    	
    	objDraw.addBall();
    	
	    	myColor = rndColor();
	    	
	    	foo = new TimeTracker(this, false);
	    	frameRate = new Timer(100, foo);
	    	frameRate.start();
	    	
    	foo = new TimeTracker(this, true);
    	fallTime = new Timer(100, foo);
    	
	}

	public void destruct() {
		objDraw.removeCircle(preX, preY, 25);
    	fallTime.stop();
    	frameRate.stop();
    	
    }
	
    public void setColor(Color c) {
    	objDraw.setColor(c);
    }
    
    public void setBGColor(Color c) {
    	objDraw.setBGColor(c);
    }
    
    public void drawObj() {
    	if(shape == "circle") {
	    	objDraw.removeCircle(preX, preY, 25);
	        objDraw.circle(x, y, 25, myColor, fill);
	        
    	}else if(shape == "rect") {
    		objDraw.rect(x, y, w, h, myColor, fill);
    	}else if(shape == "oval") {
    		objDraw.oval(x, y, w, h, myColor, fill);
    	}
    	if(shape != "player") {
    		objDraw.score();
	    	preX = x;
	        preY = y;
	        xForce();
	        yForce();
    	}else {
    		objDraw.removePlayer(preX);
    		objDraw.player(x);
    		
    	}
    	
    	if(player != null) {
    		checkPop();
    	}
    }
    
    public void position(int x, int y) {
    	this.x = x;
    	this.y = y;
    }
    
    /*
     * Whe need to calculate the forces acting on it.
     */
    public void xForce() {
    	
    	x -= velocityX;
    }
    
    public void yForce() {
    	if(increase) {
    		if(! checkCollide()) {
    			velocityY += g;
    			increase = false;
    		}
    		
    		
    	}
    	y -= velocityY;
    }
    
    public boolean checkCollide() {
    	boolean val = false;
    	if(y+25 > 400 || y-25 < -200) {
    		//velocityY = -velocityY;
    		val = true;
    		destruct();
    	}
    	if(x+25 > 600 || x-25 < 0) {
    		velocityX = -velocityX;
    	}
    	if(x+25 > 460 && x-25 < 470) {
    		if(y+25 > 150 && y-25 < 250) {
    			velocityX = -velocityX;
    		}
    	}
    	if (x+25 > 400 && x-25 < 460) {
    		if(y+25 > 204 && y-25 < 206) {
    			velocityX = -velocityX;
    		}
    	}
    	if (y+25 > 150 && y-25 < 250) {
    		if(x+25 > 460 && x-25 < 470) {
    			velocityY = -velocityY;
    		}
    	}
    	return val;
    }

	public void velocityIncr() {
		time++;
		increase = true;
	}
	
	public Color rndColor() {
		Random myRandom = new Random();
        Color myColor;
        myColor = new Color(myRandom.nextInt(256), myRandom.nextInt(256), myRandom.nextInt(256));
        
        return myColor;
    }
	
	public int rndNum(int max) {
		int value = 0;
		Random myRandom = new Random();
		value = myRandom.nextInt(max);
		
		return value;
	}
	
	public void start(int vX, int vY) {
		velocityX = vX;
		velocityY = vY;
		fallTime.start();
	}

	public void move(int i) {
		preX = x;
		objDraw.removePlayer(preX);
		x += i;
		objDraw.player(x);
	}
	
	public int getX() {
		return x;
	}
	
	public void checkPop() {
		if(y > 380) {
			if(x+25 > player.getX() && x-25 < player.getX()) {
				int t = player.getX() - x;
				if(t < 0) t = -t;
				objDraw.scoreAdd(t);
				pop();
				destruct();
			}
		}
	}
	
	public void pop() {
		objDraw.rect(0, 0, 600, 400, Color.black, true);
		TimeTracker foo = new TimeTracker(objDraw);
		Timer bar = new Timer(10, foo);
		foo.setTimeIt(bar);
		bar.start();
	}

}
